<?php
namespace app\index\controller;

use think\Request;
use think\Db;

use app\index\model\user as UserModel;

class User 
{
    //获取边缘id
    public function getBorderId($keyName, $keyWord, $page, $dataNum)
    {
        $userTab = 'dcw_fastask_user';
        $limit = ($page - 1) * $dataNum;
        $sql = '';
        if ($keyName == '') {
            $sql = "SELECT id FROM $userTab
                    LIMIT $limit
            ";
        }
        else {
            $sql = "SELECT id FROM $userTab
                    WHERE $keyName = '$keyWord'
                    LIMIT $limit
            ";
        }
        $allId = Db::query($sql);
        $borderId = $allId == NULL ? 0 : $allId[count($allId) - 1]['id'];
        return $borderId;
    }
    //获取用户的相关信息
    public function getUserAbout ($allNote) 
    {
        $questionTable = 'dcw_fastask_question';
        $answerTable = 'dcw_fastask_answer';
        foreach ($allNote as $key => $value) {
            $userToken = $value['userToken'];
            //获取提问的个数
            $sql = "SELECT COUNT(*) AS askNum FROM $questionTable
                    WHERE askerToken = '$userToken'
            ";
            $findRes = Db::query($sql);
            $allNote[$key]['askNum'] = $findRes[0]['askNum'];
            //回答的问题个数
            $sql = "SELECT COUNT(*) AS answerQueNum FROM $answerTable
                    WHERE answererToken = '$userToken'
            ";
            $findRes = Db::query($sql);
            $allNote[$key]['answerQueNum'] = $findRes[0]['answerQueNum'];
            //答案的个数
            $sql = "SELECT COUNT(*) AS answerNum FROM $answerTable 
                    WHERE answererToken = '$userToken'
            ";
            $findRes = Db::query($sql);
            $allNote[$key]['answerNum'] = $findRes[0]['answerNum'];
        }
        return $allNote;
    }
    public function getUser(Request $request)
    {
        //获取请求参数
        $keyName = $request->param('keyName');
        $keyWord = $request->param('keyWord');
        $page = $request->param('page');
        $dataNum = $request->param('dataNum');

        //初始化消息
        $message = '';
        $status = 0;

        //获取边缘id
        $borderId = $this->getBorderId($keyName, $keyWord, $page, $dataNum);

        //获取用户总数量
        $userTab = "dcw_fastask_user";
        $dataTotal = 0;
        $sql = "SELECT COUNT(*) AS dataTotal FROM $userTab";
        $res = Db::query($sql);
        $dataTotal = $res[0]['dataTotal'];

        //构造sql
        $userTab = "dcw_fastask_user";
        $sql = '';
        if ($keyName == '') {
            $sql = "SELECT * FROM $userTab
                    WHERE id > $borderId 
                    LIMIT $dataNum
            ";
        }
        else {
            $sql = "SELECT * FROM $userTab
                    WHERE id > $borderId
                    AND $keyName = '$keyWord'
                    LIMIT $dataNum
            ";
        }
        
        $allNote = Db::query($sql);
        $allNote = $this->getUserAbout($allNote);

        $status = 0;
        $message = 'get user info success.';

        //返回消息
        $data = [
            'dataTotal' => $dataTotal,
            'allNote' => $allNote
        ];
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, $data);
        return $json;
    }
    //更新用户信息
    public function updateUser(Request $request)
    {
        //获取请求参数
        $id = $request->param('id');
        $userAvatar = $request->param('userAvatar');
        $userName = $request->param('userName');
        $isExpert = $request->param('isExpert');
        $haveMoney = $request->param('haveMoney');
        $isBlack = $request->param('isBlack');
        //初始化消息
        $message = '';
        $status = 0;

        $user = UserModel::get(["id" => $id]);
        $user->userAvatar = $userAvatar;
        $user->userName = $userName;
        $user->isExpert = $isExpert;
        $user->isBlack = $isBlack;
        $user->haveMoney = $haveMoney;   

        if ($user->save()) {
            $message = 'update user info success.';
            $status = 0;
        }
        else {
            $message = 'update user info fail.';
            $status = 1;
        }

        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;
    }
    //删除用户
    public function delUser(Request $request)
    {
        //获取请求参数
        $id = $request->param('id');

        //初始化参数
        $message = '';
        $status = 0;

        //删除
        $user = UserModel::get(['id' => $id]);
        if ($user->delete()) {
            $message = 'delete user success.';
            $status = 0;
        }
        else {
            $message = 'delete user fail.';
            $status = 1;
        }
        //返回消息
        $JsonData = controller('index/JsonData');
        $json = $JsonData->getJsonData($status, $message, array());
        return $json;
    }
}